package com.example.controller;


import com.example.common.result.Result;
import com.example.entity.dto.*;
import com.example.entity.vo.WorkflowConfigVo;
import com.example.entity.vo.WorkflowDocumentHisVo;
import com.example.entity.vo.WorkflowDocumentVo;
import com.example.service.WorkflowconfigService;
import com.example.service.WorkflowdocumentService;
import com.example.untils.SecurityUtils;
import com.example.untils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lan
 * @since 2024-12-31
 */
@RestController
@RequestMapping("/api/workflow")
@Api(tags = "审批接口")
public class WorkflowController {

    @Autowired
    private WorkflowconfigService service;

    @Autowired
    private WorkflowdocumentService documentService;

    @ApiOperation("查询所所有配置")
    @PostMapping("/listConfig")
    public Result<WorkflowConfigVo.WorkflowPersonConfigResult> addConfig(@RequestBody WorkflowConfigSelectDto dto) {
        return Result.success(service.listConfig(dto));
    }

    @ApiOperation("查询配置详情")
    @GetMapping("/oneConfig")
    public Result<WorkflowConfigVo> oneConfig(@RequestParam String id) {
        return Result.success(service.oneConfig(id));
    }

    @ApiOperation("新增配置")
    @PostMapping("/addConfig")
    public Result<?> addConfig(@RequestBody WorkflowConfigAddDto dto) {
        service.addConfig(dto);
        return Result.success();
    }

    @ApiOperation("修改配置")
    @PostMapping("/updateConfig")
    public Result<?> updateConfig(@RequestBody WorkflowConfigUpdateDto dto) {
        service.updateConfig(dto);
        return Result.success();
    }

    @ApiOperation("删除配置")
    @GetMapping("/deleteConfig")
    public Result<?> deleteConfig(@RequestParam String id) {
        service.deleteConfig(id);
        return Result.success();
    }

    @ApiOperation("上报审批")
    @PostMapping("/report")
    public Result<?> report(@RequestBody WorkflowReportDto dto) {
        if (StringUtils.isEmpty(dto.getReporterNo())){
            dto.setReporterNo(SecurityUtils.getLoginUser().getUsername());
            dto.setReporterName(SecurityUtils.getLoginUser().getName());
        }
        documentService.report(dto);
        return Result.success();
    }

    @ApiOperation("取消上报")
    @GetMapping("/unReport")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workflowConfigNo",value = "审批编号",type = "string"),
            @ApiImplicitParam(name = "documentNo",value = "业务编号（前端传入 单别-单号）",type = "string")
    })
    public Result<?> unReport(@RequestParam String workflowConfigNo,@RequestParam String documentNo) {
        documentService.unReport(workflowConfigNo,documentNo);
        return Result.success();
    }

    @ApiOperation("审批")
    @PostMapping("/audit")
    public Result<?> audit(@RequestBody WorkflowAuditDto dto) {
        if (StringUtils.isEmpty(dto.getApproverNo())){
            dto.setApproverNo(SecurityUtils.getLoginUser().getUsername());
            dto.setApproverName(SecurityUtils.getLoginUser().getName());
        }
        documentService.audit(dto);
        return Result.success();
    }

    @ApiOperation("查看历史审核记录")
    @PostMapping("/reviewed")
    public Result<WorkflowDocumentHisVo.WorkflowDocumentHisResult> reviewed(@RequestBody WorkflowSelectDto dto) {
        return Result.success(documentService.reviewed(dto));
    }

    @ApiOperation("查看现在要审核的记录")
    @PostMapping("/underReview")
    public Result<WorkflowDocumentVo.WorkflowDocumentResult> underReview(@RequestBody WorkflowSelectDto dto) {
        return Result.success(documentService.underReview(dto));
    }
}
